#include "executor.h"

#define t_user
#define t_book
#define t_borrow

static void test_insert(db_t *db);
static void test_update(db_t *db);
static void test_delete(db_t *db);
static void test_select(db_t *db);

int main()
{
    db_t *db = createDB();

    test_insert(db);
    // test_select(db);
    execute(db, NULL, ".flush DBfile/test_db", 1);

    /*
    Travel(db->book->tree);
    TravelData(db->book->tree);
    printTable(db->book);
    */

    destroyDB(db);
    return 0;
}

static void test_insert_to_user(db_t *db)
{
    int index, last_index;
    ASTNode *ast_node;
    PlanNode *plan_node;
    record_t record;
    char input_buf[200];

    int count = 30;
    while (count--) {
        // int id = rand() % 500;
        int id = count + 1;

        /* user table insert */
        index = 0; last_index = 0;
        generateRecord(id, &record, USER);
        sprintf(input_buf, "insert into user values (%hd,'%s','%s')", 
                record.val_user.user_id, record.val_user.name, record.val_user.email);

        ast_node = parseSQL(input_buf, &index, &last_index);
        plan_node = buildPlanTree(ast_node);
        execute(db, plan_node, NULL, 0);

        destroyAST(ast_node);
        destroyPlanTree(plan_node);
    }
}

static void test_insert_to_book(db_t *db)
{
    int index, last_index;
    ASTNode *ast_node;
    PlanNode *plan_node;
    record_t record;
    char input_buf[200];

    int count = 30;
    while (count--) {
        // int id = rand() % 500;
        int id = count + 1;

        /* book table insert */
        index = 0; last_index = 0;
        generateRecord(id, &record, BOOK);
        sprintf(input_buf, "insert into book values (%hd,%hd,%hd,%hhd,'%s','%s')", 
            record.val_book.book_id, record.val_book.quantity, record.val_book.available,
            record.val_book.category, record.val_book.title, record.val_book.author);

        ast_node = parseSQL(input_buf, &index, &last_index);
        plan_node = buildPlanTree(ast_node);
        execute(db, plan_node, NULL, 0);

        destroyAST(ast_node);
        destroyPlanTree(plan_node);
    }
}

static void test_insert_to_borrow(db_t *db)
{
    int index, last_index;
    ASTNode *ast_node;
    PlanNode *plan_node;
    record_t record;
    char input_buf[200];

    int count = 30;
    while (count--) {
        // int id = rand() % 500;
        int id = count + 1;

        /* borrow table insert */
        index = 0; last_index = 0;
        generateRecord(id, &record, BORROW);
        sprintf(input_buf, "insert into borrow values (%hd,%hd,%hd,'%hd-%hhd-%hhd','%hd-%hhd-%hhd',%hhd)", 
            record.val_borrow.borrow_id, record.val_borrow.book_id, record.val_borrow.user_id,
            record.val_borrow.borrow_date.year, record.val_borrow.borrow_date.month, record.val_borrow.borrow_date.day, 
            record.val_borrow.return_date.year, record.val_borrow.return_date.month, record.val_borrow.return_date.day, 
            record.val_borrow.status);

        ast_node = parseSQL(input_buf, &index, &last_index);
        plan_node = buildPlanTree(ast_node);
        execute(db, plan_node, NULL, 0);

        destroyAST(ast_node);
        destroyPlanTree(plan_node);
    }
}

static void test_insert(db_t *db)
{
#ifdef t_user
    test_insert_to_user(db);
#endif /* t_user */

#ifdef t_book
    test_insert_to_book(db);
#endif /* t_book */

#ifdef t_borrow
    test_insert_to_borrow(db);
#endif /* t_borrow */
}

void test_update(db_t *db)
{

}

void test_delete(db_t *db)
{

}

void test_select(db_t *db)
{
    int index, last_index;
    ASTNode *ast_node;
    PlanNode *plan_node;
    char input_buf[200];

    index = 0; last_index = 0;

#ifdef t_user
    // sprintf(input_buf, "select * from user");
    // sprintf(input_buf, "select email, name from user");
    sprintf(input_buf, "select email, name from user where user_id > 15 or user_id <= 3");

    ast_node = parseSQL(input_buf, &index, &last_index);
    plan_node = buildPlanTree(ast_node);
    execute(db, plan_node, NULL, 0);
#endif /* t_user */

#ifdef t_book
    sprintf(input_buf, "select * from book");
    // sprintf(input_buf, "select email, name from user");
    // sprintf(input_buf, "select email, name from user where user_id > 15 or user_id <= 3");

    ast_node = parseSQL(input_buf, &index, &last_index);
    plan_node = buildPlanTree(ast_node);
    execute(db, plan_node, NULL, 0);
#endif /* t_book */

#ifdef t_borrow
    sprintf(input_buf, "select * from borrow");

    ast_node = parseSQL(input_buf, &index, &last_index);
    plan_node = buildPlanTree(ast_node);
    execute(db, plan_node, NULL, 0);
#endif /* t_borrow */

    destroyAST(ast_node);
    destroyPlanTree(plan_node);
}